package realphone

import (
	"github.com/jinzhu/gorm"
	"time"
)

//CREATE TABLE `adi_certificate_repository` (
//  `id` bigint NOT NULL AUTO_INCREMENT,
//  `md5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '证书md5',
//  `root_cert_md5` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '根证书md5',
//  `certificate` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci COMMENT '证书内容',
//  `certificate_level` tinyint(1) DEFAULT '4' COMMENT '证书层级',
//  `use_total` int DEFAULT '0' COMMENT '使用次数',
//  `delete_flag` tinyint(1) DEFAULT '0' COMMENT '是否删除（1：已删除；0：未删除）',
//  `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
//  `create_by` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '创建人',
//  `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
//  `update_by` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '修改人',
//  PRIMARY KEY (`id`) USING BTREE,
//  KEY `idx_md5` (`md5`) USING BTREE
//) ENGINE=InnoDB AUTO_INCREMENT=131049 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='ADI证书库';

type CertificateRepository struct {
	Id               int64     `json:"id" gorm:"column:id"`
	Md5              string    `json:"md5" gorm:"column:md5"`
	RootCertMd5      string    `json:"root_cert_md5" gorm:"column:root_cert_md5"`
	Certificate      string    `json:"certificate" gorm:"column:certificate"`
	CertificateLevel int       `json:"certificate_level" gorm:"column:certificate_level"`
	UseTotal         int       `json:"use_total" gorm:"column:use_total"`
	DeleteFlag       int       `json:"delete_flag" gorm:"column:delete_flag"`
	CreateTime       time.Time `json:"create_time" gorm:"column:create_time"`
	CreateBy         string    `json:"create_by" gorm:"column:create_by"`
	UpdateTime       time.Time `json:"update_time" gorm:"column:update_time"`
	UpdateBy         string    `json:"update_by" gorm:"column:update_by"`
}

func (a *CertificateRepository) TableName() string {
	return "adi_certificate_repository"
}
func (a *CertificateRepository) Create(db *gorm.DB) error {
	return db.Table(a.TableName()).Create(a).Error
}
func (a *CertificateRepository) Update(db *gorm.DB) error {
	return db.Table(a.TableName()).Save(a).Error
}
func (a *CertificateRepository) GetOneRandByLevel(db *gorm.DB) error {
	return db.Table(a.TableName()).Where("certificate_level = ?", a.CertificateLevel).Order("RAND()").First(a).Error
}
